<?php


namespace App\Utils\Server;


use webSocket\ServerHandle;
use webSocket\Service\PushService;

class SocketHandle extends ServerHandle
{

    /**
     * 对客户端发送的登录消息进行处理
     * @param $fd
     * @param $data
     */
    public function message_login($fd, $data) {
//        PushService::pushToAllOutMeAsync($fd, $this->results("Login", 1, [
//            'message' => "用户 {$fd} 登录"
//        ]));
        PushService::pushToFdAsync($fd, $this->results("Login", 1, [
            'message' => "用户 {$fd} 登录"
        ]));
    }

    /**
     * 异步消息请求处理
     * @param $fd
     * @param $data
     */
    public function message_async($fd, $data) {
        PushService::pushToAllOutMeAsync($fd, $this->results("async", 1, [
            'message' => "异步消息发送成功"
        ]));

//        PushService::pushToAllAsync($this->results("async", 1, [
//            'message' => "异步任务发送成功"
//        ]));
    }

    /**
     * 客户端发送订阅事件
     * @param $fd
     * @param $data
     */
    public function message_publish($fd, $data) {
        $data = is_string($data) ? $data : json_encode($data, JSON_UNESCAPED_UNICODE);
        $this->publish("packet.rob", [
            'fd' => $fd,
            'data' => $data
        ]);
    }

    /**
     * 对客户端的其它消息发送信息
     * @param $fd
     * @param $data
     * @return array
     */
    public function message_chat($fd, $data) {
        return $this->results("chat", 1, "消息发送成功");
    }

    /**
     * 用户关闭了连接
     * @param $fd
     */
    public function close($fd)
    {
        \Log::info($fd." is close !");
    }
}
